package kafka.client.util;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @author lichao
 *
 */
public class SMS {
	static Logger logger = LoggerFactory.getLogger(SMS.class);
	private static final String smsUrl = "http://ms.58control.cn/sms/warn";
	private static final List<String> defaultPhone = Lists.newArrayList("13522482231", "13811583416", "15010282098");

	public static void send(List<String> phones, String message, int silentStartTime, int silentEndTime) {

		int now = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

		if (silentStartTime < silentEndTime && now >= silentStartTime && now < silentEndTime) {// 同一天
			return;
		} else if (silentStartTime > silentEndTime && (now >= silentStartTime || now < silentEndTime)) {// 隔天
			return;
		} else {// start = end 不过滤

		}
		for (String phone : phones) {
			Map<String, String> params = Maps.newHashMap();
			params.put("priority", "G");
			params.put("function", "1025");
			params.put("mo", phone);
			params.put("ms", "【IMC-Kafka】" + message);
			String response = HttpUtils.getStringPost(smsUrl, params);
			if (!"0".equals(response)) {
				logger.info("sms error:" + response);
			}
		}
	}

	public static void defaultSend(String message) {
		send(defaultPhone, message, 22, 8);
	}
	

	public static void defaultSend(List<String> phones,String message) {
		send(phones, message, 22, 8);
	}
	
	public static void defaultSend(String message, int silentStartTime, int silentEndTime) {
		send(defaultPhone, message, silentStartTime, silentEndTime);
	}
}
